package ui.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;
import net.ck.hzq.WebviewActivity;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import base.BaseAct;
import butterknife.BindView;
import event.NetErrorEvent;
import ui.activity.login.biz.RegisterBiz;
import ui.activity.login.component.DaggerRegisterComponent;
import ui.activity.login.contract.RegisterContract;
import ui.activity.login.module.RegisterModule;
import ui.activity.login.presenter.RegisterPresenter;
import widget.ClearEditText;

/**
 * 注册页面
 */
public class RegisterAct extends BaseAct implements RegisterContract.View {


    @Inject
    RegisterPresenter presenter;
    @Inject
    RegisterBiz biz;

    @BindView(R.id.edit_register_name)
    ClearEditText registerName;
    @BindView(R.id.edit_register_phone)
    ClearEditText registerPhone;
    @BindView(R.id.register_txt_getphonecode)
    TextView getphonecode;
    @BindView(R.id.edit_register_verificationcode)
    ClearEditText registerVerificationcode;
    @BindView(R.id.edit_register_passwrod)
    ClearEditText passwrod;
    @BindView(R.id.edit_register_inputpasswordagain)
    ClearEditText passwordagain;
    @BindView(R.id.register_checkbox)
    CheckBox registerCheckbox;
    @BindView(R.id.txt_register_word)
    TextView registerWord;
    @BindView(R.id.but_regitser)
    Button regitser;

    private CountDownTimer timer;


    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_register);
    }

    @Override
    protected void setup() {
        DaggerRegisterComponent.builder().registerModule(new RegisterModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.register), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        setKeyboard();
        RxView.clicks(getphonecode)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.getPhoneCode(registerPhone.getText().toString().trim()));
        RxView.clicks(registerWord)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    Intent intent = new Intent(RegisterAct.this, WebviewActivity.class);
                    intent.putExtra("url", "file:///android_asset/echart/xy.html");
                    intent.putExtra("type", "1");
                    startActivity(intent);
                });
        RxView.clicks(regitser)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.getRegisterInfo(registerPhone.getText().toString().trim(), passwrod.getText().toString().trim(),
                        passwordagain.getText().toString().trim(), registerName.getText().toString().trim(),
                        registerCheckbox.isChecked(), registerVerificationcode.getText().toString().trim()));

    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    private void toWeb() {

    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public void upDateUI() {
        if (timer != null) {
            timer.cancel();
        }
        getphonecode.setEnabled(false);
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                getphonecode.setText(l / 1000 + "秒后重发");
            }

            @Override
            public void onFinish() {
                getphonecode.setEnabled(true);
                getphonecode.setText("获取验证码");
            }
        };
        timer.start();
    }
}

